<template>
  <view class="chat-container">
    <scroll-view
      class="chat-content"
      scroll-y="true"
      :scroll-top="anonymousStore.scrollTop"
      @scrolltoupper="anonymousStore.loadMoreMessages"
      :scroll-with-animation="true"
      ref="chatScroll"
    >
      <!-- 欢迎信息 -->
      <view class="welcome-message" v-if="messages.length === 0">
        <text class="title">匿名对话</text>
      </view>
      <!-- 消息列表 -->
      <view class="messages">
        <block v-for="(message, index) in messages" :key="index">
          <!-- 用户消息 -->
          <view class="message-item user-message" v-if="message.role === 'user'">
            <view class="message-content">{{ message.content }}</view>
          </view>
          <!-- AI消息 -->
          <view class="message-item ai-message" v-else>
            <view class="message-content">
              <text>{{ message.content }}</text>
              <text
                class="typing-dots"
                v-if="anonymousStore.isStreaming && index === messages.length - 1"  
                >...</text
              >
            </view>
          </view>
        </block>
      </view>
    </scroll-view>

    <!-- 底部输入区域 -->
    <view class="input-area">
      <input
        class="input-box"
        type="text"
        v-model="anonymousStore.inputValue"
        :placeholder="anonymousStore.isStreaming ? '正在回复中......' : '请输入您的问题...'"
        :disabled="anonymousStore.isStreaming"
        @confirm="anonymousStore.handleSend"
        confirm-type="send"
      />
      <button
        class="send-btn"
        @tap="anonymousStore.handleSend"
        :disabled="anonymousStore.isStreaming || !anonymousStore.inputValue.trim()"
      >
        发送
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref, nextTick, onMounted } from "vue";
import { useAnonymousStore } from "@/store/modules/chat/anonymous";
const anonymousStore = useAnonymousStore();
const messages = ref([]);
onMounted(() => {
  console.log("对话界面挂载成功");
  anonymousStore.messages = [];
  messages.value = anonymousStore.messages;
});
</script>

<style>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  /* background: linear-gradient(180deg, #f6f9fc 0%, #e8f4ff 100%); */
}

.chat-content {
  flex: 1;
  padding: 30rpx;
  padding-bottom: 180rpx;
  overflow-y: auto;
  box-sizing: border-box;
}

.welcome-message {
  display: flex;
  justify-content: center; /* 新增：水平居中 */
  align-items: center; /* 新增：垂直居中 */
  text-align: center;
  padding: 80rpx 0;
  animation: fadeIn 0.5s ease;
  height: 100%;
  flex-direction: column;
}

.logo {
  height: 250rpx;
  width: 300rpx;
  margin-bottom: 30rpx;
  border-radius: 50%;
  box-shadow: 0 8rpx 20rpx rgba(0, 122, 255, 0.1);
}

.title {
  background: linear-gradient(to right, #2193b0, #3498db, #6dd5ed);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent !important;
  font-weight: bold;
  animation: gradient 3s ease infinite;
  background-size: 200% auto;
  font-size: 30rpx;
  margin-bottom: 20px;
}

.subtitle {
  font-size: 28rpx;
  color: #666;
}

.messages {
  padding-bottom: 30rpx;
  width: 100%;
  box-sizing: border-box;
}

.message-item {
  margin: 30rpx 0;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
}

.message-content {
  max-width: 75%;
  padding: 24rpx;
  border-radius: 24rpx;
  font-size: 30rpx;
  line-height: 1.6;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.user-message {
  align-items: flex-end;
}

.user-message .message-content {
  background: linear-gradient(135deg, #007aff 0%, #0062cc 100%);
  color: #fff;
  border-bottom-right-radius: 4rpx;
}

.ai-message {
  align-items: flex-start;
}

.ai-message .message-content {
  background: #fff;
  color: #333;
  border-bottom-left-radius: 4rpx;
  border: 1rpx solid #f0f0f0;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin: 0 20rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.typing-dots {
  display: inline-block;
  animation: typing 1s infinite;
  margin-left: 4px;
}

.input-area {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 30rpx;
  background-color: #fff;
  border-top: 1rpx solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));
  padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
}

.input-box {
  flex: 1;
  height: 90rpx;
  padding: 0 30rpx;
  border: 1rpx solid #e0e0e0;
  border-radius: 15rpx;
  margin-right: 30rpx;
  background-color: #fff;
  font-size: 30rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.input-box:focus {
  border-color: #007aff;
}

.send-btn {
  width: 120rpx;
  height: 90rpx;
  line-height: 90rpx;
  background: linear-gradient(135deg, #007aff 0%, #0062cc 100%);
  color: #fff;
  border-radius: 15%;
  font-size: 30rpx;
  text-align: center;
  box-shadow: 0 4rpx 12rpx rgba(0, 122, 255, 0.2);
  transition: all 0.3s ease;
}

.send-btn[disabled] {
  background: #ccc;
  box-shadow: none;
}

.send-icon {
  width: 40rpx;
  height: 40rpx;
}

.top-bar {
  top: 0;
  left: 0;
  right: 0;
  height: 95rpx;
  width: 100%;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 36rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

@keyframes typing {
  0% {
    opacity: 0.3;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0.3;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
